package com.zzk.lanouhn.myapplication1.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.view.GoodView;
import com.zzk.lanouhn.myapplication1.view.InvestDetailedPopComment;
import com.zzk.lanouhn.myapplication1.view.SelectPicPopupWindow;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Invest的二级页面
 * Created by lanouhn on 16/8/26.
 */
public class InvestOneDetailed extends AppCompatActivity {
    private WebView webView;

    private int mCurrentItem;
    private int mCurrentChooseItem;

    //接受传过来的的值
    private String dataString, lunbo, lunboShu, shipin;

    //各种控件
    private ImageView iv_fanHui;
    private VideoView vv;
    private RelativeLayout rl;
    private ImageView iv_popWindow, iv_dianzan, iv_dianzan_quxiao, iv_cai, iv_cai_quxiao, iv_pinglun;
    private TextView pinlun;
    private RelativeLayout yejian;

    private SelectPicPopupWindow selectPicPopupWindow;
    private InvestDetailedPopComment popComment;

    private SharedPreferences sp;
    private String login;


    //点赞的动画效果
    private GoodView mGoodView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailed_invest_one);

        iv_fanHui = (ImageView) findViewById(R.id.detailed_invest_one_iv1);
        vv = (VideoView) findViewById(R.id.detailed_invest_one_vv);
        rl = (RelativeLayout) findViewById(R.id.detailed_invest_one_rl);
        iv_popWindow = (ImageView) findViewById(R.id.detailed_invest_one_iv2);
        pinlun = (TextView) findViewById(R.id.detailed_invest_one_tv);
        //跳转评论的二级页面
        iv_pinglun = (ImageView) findViewById(R.id.detailed_invest_one_iv6);
        //点赞
        iv_dianzan = (ImageView) findViewById(R.id.detailed_invest_one_iv4);
        //取消点赞
        iv_dianzan_quxiao = (ImageView) findViewById(R.id.detailed_invest_one_iv4_1);
        //踩
        iv_cai = (ImageView) findViewById(R.id.detailed_invest_one_iv5);
        //取消踩
        iv_cai_quxiao = (ImageView) findViewById(R.id.detailed_invest_one_iv5_1);
        //夜间
        yejian = (RelativeLayout) findViewById(R.id.detailed_invest_rl_yejian);
        mGoodView = new GoodView(this);

        sp = getSharedPreferences("loginpic", Context.MODE_PRIVATE);
        login = sp.getString("shifou", "false");

        webView = (WebView) findViewById(R.id.detailed_invest_one_wedView);
        //设置webView的属性
        final WebSettings webSettings = webView.getSettings();
        //能够执行JavaScript脚本
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        //webSettings.setAllowFileAccess(true);
        //设置支持缩放
        //webSettings.setBuiltInZoomControls(true);
        //视频播放设置
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setUseWideViewPort(true);

        lunbo = getIntent().getStringExtra("lunbo");
        lunboShu = getIntent().getStringExtra("lunboShu");

        dataString = getIntent().getStringExtra("shishi");
        //视频
        Intent intent = getIntent();
        shipin = intent.getStringExtra("shipin");

        if (lunboShu != null) {
            webView.loadUrl("http://lol.zhangyoubao.com/mobiles/item/" + lunboShu + "?user_id=&token=&i_=" +
                    "EAC1B788-00BC-454A-A9B9-460852CFC011&t_=1438745027&p_=17426&v_=40050303&d_=" +
                    "ios&osv_=8.3&version=0&a_=lol&size=middle");
        }

        if (lunbo != null) {
            //加载需要显示的网页
            webView.loadUrl("http://lol.zhangyoubao.com/mobiles/item/" + lunbo + "?user_id=&token=&i_=" +
                    "EAC1B788-00BC-454A-A9B9-460852CFC011&t_=1438745027&p_=17426&v_=40050303&d_=" +
                    "ios&osv_=8.3&version=0&a_=lol&size=middle");
        }

        if (dataString != null) {
            webView.loadUrl("http://lol.zhangyoubao.com/mobiles/item/" + dataString + "?user_id=&token=&i_=EAC1B788-" +
                    "00BC-454A-A9B9-460852CFC011&t_=1438745542&p_=18971&v_=40050303&d_=ios&osv_=8.3&version=" +
                    "0&a_=lol&size=middle");
            //播放视频
            if (shipin.length() > 20) {
                Uri uri = Uri.parse(shipin);
                webView.setVisibility(View.GONE);
                //rl.setVisibility(View.GONE);
                vv.setMediaController(new MediaController(InvestOneDetailed.this));
                vv.setVisibility(View.VISIBLE);
                vv.setVideoURI(uri);
                vv.start();
                vv.requestFocus();
                //webView.loadUrl(shipin);
//                webView.loadUrl("http://lol.zhangyoubao.com/mobiles/item/" + dataString + "?user_id=&token=&i_=" +
//                        "EAC1B788-00BC-454A-A9B9-460852CFC011&t_=1438745874&p_=19967&v_=40050303&d_=" +
//                        "ios&osv_=8.3&version=0&a_=lol&size=middle");
//                webView.setOnTouchListener(new View.OnTouchListener() {
//                    @Override
//                    public boolean onTouch(View v, MotionEvent event) {
//                        webView.loadUrl(shipin);
//                        return false;
//                    }
//                });

            } else {
                //加载需要显示的网页
                //item需要加载的
                webView.loadUrl("http://lol.zhangyoubao.com/mobiles/item/" + dataString + "?user_id=&token=&i_=EAC1B788-" +
                        "00BC-454A-A9B9-460852CFC011&t_=1438745542&p_=18971&v_=40050303&d_=ios&osv_=8.3&version=" +
                        "0&a_=lol&size=middle");
            }
        }


        //设置web视图
        webView.setWebViewClient(new myWebViewClient());
        initDate();
    }


    private void initDate() {
        //跳转评论的二级页面
        iv_pinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(InvestOneDetailed.this, InvestPingLunDetails.class);
                startActivity(intent);
            }
        });

        //返回
        iv_fanHui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //弹出的选项，右上角pop
        iv_popWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int[] location = new int[2];
                v.getLocationOnScreen(location);

                selectPicPopupWindow = new SelectPicPopupWindow(InvestOneDetailed.this, itemOnClick);
                //在控件下面
                selectPicPopupWindow.showAsDropDown(iv_popWindow);
                //自定义pop的位置
//                selectPicPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1] -
//                        selectPicPopupWindow.getHeight());
            }
        });
        //评论pop
        pinlun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (login.equals("false")) {
                    Intent intent = new Intent(InvestOneDetailed.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    popComment = new InvestDetailedPopComment(InvestOneDetailed.this, itemOnClickComment);
                    popComment.showAtLocation(InvestOneDetailed.this.findViewById(R.id.detailed_invest_one_rl),
                            Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                }
            }
        });


        //点赞
        iv_dianzan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //((ImageView)v).setImageResource(R.mipmap.invest_detailed_dianzan_hong);
                if (login.equals("false")) {
                    Intent intent = new Intent(InvestOneDetailed.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    iv_dianzan_quxiao.setVisibility(View.VISIBLE);
                    iv_dianzan.setVisibility(View.INVISIBLE);
                    mGoodView.setText("+1");
                    mGoodView.show(v);
                    iv_cai.setEnabled(false);
                }
            }
        });
        iv_dianzan_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (login.equals("false")) {
                    Intent intent = new Intent(InvestOneDetailed.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    iv_dianzan_quxiao.setVisibility(View.INVISIBLE);
                    iv_dianzan.setVisibility(View.VISIBLE);
                    mGoodView.setText("-1");
                    mGoodView.show(v);
                    iv_cai.setEnabled(true);
                }
            }
        });
        //踩
        iv_cai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //((ImageView)v).setImageResource(R.mipmap.detailed_cai_hong);
                if (login.equals("false")) {
                    Intent intent = new Intent(InvestOneDetailed.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    iv_cai.setVisibility(View.INVISIBLE);
                    iv_cai_quxiao.setVisibility(View.VISIBLE);
                    mGoodView.setText("+1");
                    mGoodView.show(v);
                    iv_dianzan.setEnabled(false);
                }
            }
        });
        //取消踩
        iv_cai_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (login.equals("false")) {
                    Intent intent = new Intent(InvestOneDetailed.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    iv_cai.setVisibility(View.VISIBLE);
                    iv_cai_quxiao.setVisibility(View.INVISIBLE);
                    mGoodView.setText("-1");
                    mGoodView.show(v);
                    iv_dianzan.setEnabled(true);
                }
            }
        });

    }

    //PopupWindow里的点击事件
    private View.OnClickListener itemOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                //点击分享的按钮
                case R.id.invest_pop_fen:
                    //调用分享
                    showShare();
                    selectPicPopupWindow.dismiss();
                    break;
                //点击改变字体大小
                case R.id.invest_pop_zi:
                    AlertDialog.Builder builder = new AlertDialog.Builder(InvestOneDetailed.this);
                    String[] items = new String[]{"超大号字体", "大号字体", "正常字体", "小号字体", "超小号字体"};
                    builder.setTitle("字体判断");
                    builder.setSingleChoiceItems(items, mCurrentItem, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mCurrentChooseItem = which;
                        }
                    });

                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        WebSettings settings = webView.getSettings();

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (mCurrentChooseItem) {
                                case 0:
                                    settings.setTextSize(WebSettings.TextSize.LARGEST);
                                    break;
                                case 1:
                                    settings.setTextSize(WebSettings.TextSize.LARGER);
                                    break;
                                case 2:
                                    settings.setTextSize(WebSettings.TextSize.NORMAL);
                                    break;
                                case 3:
                                    settings.setTextSize(WebSettings.TextSize.SMALLER);
                                    break;
                                case 4:
                                    settings.setTextSize(WebSettings.TextSize.SMALLEST);
                                    break;
                            }
                            mCurrentItem = mCurrentChooseItem;

                        }
                    });
                    builder.setNegativeButton("取消", null);
                    builder.show();
                    selectPicPopupWindow.dismiss();
                    break;
                //夜间模式
                case R.id.invest_pop_ye:
                    if (yejian.getVisibility() == View.GONE) {
                        yejian.setVisibility(View.VISIBLE);
                    } else {
                        yejian.setVisibility(View.GONE);
                    }
                    selectPicPopupWindow.dismiss();
                    break;
                //收藏
                case R.id.invest_pop_shu:
                    if (login.equals("false")) {
                        Intent intent = new Intent(InvestOneDetailed.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(InvestOneDetailed.this, "收藏成功", Toast.LENGTH_SHORT).show();
                        selectPicPopupWindow.dismiss();
                    }
                    break;
                //刷新
                case R.id.invest_pop_shua:
                    webView.reload();
                    selectPicPopupWindow.dismiss();
                    break;


            }

        }
    };
    //点击分享后的点击事件
//    private View.OnClickListener itemOnClickFen = new View.OnClickListener() {
//        @Override
//        public void onClick(View v) {
//            switch (v.getId()) {
//
//            }
//        }
//    };
    //评论里的点击事件
    private View.OnClickListener itemOnClickComment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.pop_invest_detailed_comment_tv2:
                    finish();

            }
        }
    };

    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("标题");
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(this);
    }


    //Web视图
    private class myWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

}
